﻿using HslCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormModbusRtuTool : Form
    {
        SerialModBusRtu m_ModBus;
        public FormModbusRtuTool(SerialModBusRtu _ModBus)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DoubleBuffered = true;//避免时间显示控件闪烁
            InitializeComponent();
            m_ModBus = _ModBus;
        }


        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormModbusRtuTool_Load(object sender, EventArgs e)
        {
            //加载串口信息至控件
            m_ModBus.LoadSerialPort(Combox_COM, Combox_Paud, Combox_Parity, Combox_StopBits, Combox_DataBits);
            //判断串口是否已经打开
            if (m_ModBus.PLC != null)
            {
                if (m_ModBus.PLC.IsOpen())
                {
                    panel_State.BackColor = Color.Green;
                    button_Open.Enabled = false;
                    button_Close.Enabled = true;

                    m_ModBus.ReadModBusParameter();
                    Combox_COM.Text = m_ModBus.COM.ToString();
                    Combox_Paud.Text = m_ModBus.BaudRate.ToString();
                    Combox_Parity.Text = m_ModBus.BarityBit.ToString();
                    Combox_DataBits.Text = m_ModBus.DataBits.ToString();
                    Combox_StopBits.Text = m_ModBus.StopBits.ToString();

                }
                else
                {
                    panel_State.BackColor = Color.Red;
                    button_Open.Enabled = true;
                    button_Close.Enabled = false;
                }
            }
            else
            {
                panel_State.BackColor = Color.Red;
                button_Open.Enabled = true;
                button_Close.Enabled = false;
            }
        }

        /// <summary>
        /// 保存串口数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                m_ModBus.COM = this.Combox_COM.Text;
                m_ModBus.BaudRate = this.Combox_Paud.Text.ToInt32();
                m_ModBus.DataBits = this.Combox_DataBits.Text.ToInt32();
                m_ModBus.BarityBit = this.Combox_Parity.Text.ToInt32();
                m_ModBus.StopBits = this.Combox_StopBits.Text.ToInt32();
                m_ModBus.WriteModBusParameter();
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Open_Click(object sender, EventArgs e)
        {
            m_ModBus.OpenSerial(Combox_COM.Text, Convert.ToInt32(Combox_Paud.Text), Convert.ToInt32(Combox_DataBits.Text), Convert.ToInt32(Combox_StopBits.Text), Convert.ToInt32(Combox_Parity.Text));
            if (m_ModBus.PLC.IsOpen())
            {
                button_Open.Enabled = false;
                button_Close.Enabled = true;
                panel_State.BackColor = Color.Green;
                m_ModBus.State = true;
            }


        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Close_Click(object sender, EventArgs e)
        {
            if (m_ModBus.PLC.IsOpen())
            {
                m_ModBus.ClosePlcServer();
                button_Open.Enabled = true;
                button_Close.Enabled = false;
                panel_State.BackColor = Color.Red;
                m_ModBus.State = false;
            }
        }


        private void btn_Read_Click(object sender, EventArgs e)
        {
            if (m_ModBus.PLC.IsOpen())
            {

                try
                {
                    if (Com_ReadDataType.Text == "bool")
                    {
                        if (txt_ReadAddress.Text.Length > 0)
                        {
                            try
                            {
                                OperateResult<bool> result = m_ModBus.PLC.ReadBool(txt_ReadAddress.Text);
                                if (result.IsSuccess)
                                {
                                    bool data = result.Content;
                                    textBox2.Text += data.ToString();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }

                        }
                        else
                        {
                            MessageBox.Show("请输入地址");
                        }
                    }
                    if (Com_ReadDataType.Text == "int")
                    {
                        if (txt_ReadAddress.Text.Length > 0)
                        {
                            try
                            {
                                OperateResult<int> result = m_ModBus.PLC.ReadInt32(txt_ReadAddress.Text);
                                if (result.IsSuccess)
                                {
                                    int data = result.Content;
                                    textBox2.Text += data.ToString();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }

                        }
                        else
                        {
                            MessageBox.Show("请输入地址");
                        }

                    }
                    if (Com_ReadDataType.Text == "short")
                    {
                        if (txt_ReadAddress.Text.Length > 0)
                        {
                            try
                            {
                                OperateResult<short> result = m_ModBus.PLC.ReadInt16(txt_ReadAddress.Text);
                                if (result.IsSuccess)
                                {
                                    short data = result.Content;
                                    textBox2.Text += data.ToString();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入地址");
                        }
                    }
                    if (Com_ReadDataType.Text == "long")
                    {
                        if (txt_ReadAddress.Text.Length > 0)
                        {
                            try
                            {
                                OperateResult<long> result = m_ModBus.PLC.ReadInt64(txt_ReadAddress.Text);
                                if (result.IsSuccess)
                                {
                                    long data = result.Content;
                                    textBox2.Text += data.ToString();
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("请输入地址");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
            else
            {
                MessageBox.Show("串口未打开！");
            }
        }

        /// <summary>
        /// 将数据写入PLC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Write_Click(object sender, EventArgs e)
        {
            if (m_ModBus.PLC.IsOpen())
            {
                if (txt_WriteAddress.Text != "")
                {
                    try
                    {

                        if (Com_WriteDataType.Text == "bool")
                        {
                            try
                            {
                                bool data = Convert.ToBoolean(textBox1.Text);
                                m_ModBus.PLC.Write(txt_WriteAddress.Text, (bool)data);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                        if (Com_WriteDataType.Text == "int")
                        {
                            try
                            {
                                int data = Convert.ToInt32(textBox1.Text);
                                m_ModBus.PLC.Write(txt_WriteAddress.Text, (int)data);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }

                        if (Com_WriteDataType.Text == "short")
                        {
                            try
                            {
                                int data = Convert.ToInt32(textBox1.Text);
                                m_ModBus.PLC.Write(txt_WriteAddress.Text, (short)data);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }

                        }
                        if (Com_WriteDataType.Text == "long")
                        {
                            try
                            {
                                int data = Convert.ToInt32(textBox1.Text);
                                m_ModBus.PLC.Write(txt_WriteAddress.Text, (long)data);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("写入PLC 地址不能为空！！");
                }
            }
            else
            {
                MessageBox.Show("串口未打开！");
            }
        }
    }
}

